// 取值范围 h 0-360°, s 0-100%, v 0-100%
// rgb: Array
export const rgbToHsv = (rgb) => {
    let [h, s, v] = [0, 0, 0]
    let [r, g, b] = [rgb[0] / 255, rgb[1] / 255, rgb[2] / 255]
    let sortRgb = rgb.map(i => i).sort((a, b) => a - b)
    let min = sortRgb[0]
    let max = sortRgb[2]

    // h
    min == max ? h = 0 :
    max == r && g >= b ? h = 60 * ((g - b) / (max - min)) + 0 :
    max == r && g < b ? h = 60 * ((g - b) / (max - min)) + 360 :
    max == g ? h = 60 * ((b - r) / (max - min)) + 120 :
    max == b ? h = 60 * ((r - g) / (max - min)) + 240 : null
    
    // s
    max == 0 ? s = 0 : s = 1 - (min / max)

    // v
    v = max / 255

    h = parseInt(h)
    s = parseInt(s * 100)
    v = parseInt(v * 100)

    return [h, s, v]
}

export const hsvToRgb = (hsv) => {
    let [h, s, v] = [hsv[0], hsv[1] / 100, hsv[2] / 100]
    let [r, g, b] = [0, 0, 0]
    let i = parseInt((h / 60) % 6)
    let f = h / 60 - i
    let p = v * (1 - s)
    let q = v * (1 - f * s)
    let t = v * (1 - (1 - f) * s)
    i == 0 ? [r, g, b] = [v, t, p] :
    i == 1 ? [r, g, b] = [q, v, p] :
    i == 2 ? [r, g, b] = [p, v, t] :
    i == 3 ? [r, g, b] = [p, q, v] :
    i == 4 ? [r, g, b] = [t, p, v] :
    i == 5 ? [r, g, b] = [v, p, q] : null

    r = parseInt(r * 255)
    g = parseInt(g * 255)
    b = parseInt(b * 255)

    return [r, g, b]
}

// export default { rgbToHsv, hsvToRgb }

